/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rowingmanager.util.command;

import java.io.IOException;
import java.util.ArrayList;
import org.apache.log4j.Logger;

/**
 *
 * @author simon
 */
public class ImageResizer {
    private static Logger log = Logger.getLogger(ImageResizer.class);
    private static String CONVERT_PROG =
    "C:\\tools\\ImageMagick-6.5.5-Q16\\convert.exe";
    private ArrayList<String> command;
    private String fileOut;
    public ImageResizer(String fileIn,String fileOut){
        log.info("Inside imageresizer constructor!");
        command = new ArrayList<String>(10);
        command.add(CONVERT_PROG);
        command.add(fileIn);
        this.fileOut = fileOut;
    }
    
    public boolean createAvatar(){
        command.add("-resize");
        command.add("120x100");
        command.add(fileOut);

        String [] comm = (String [])command.toArray(new String[1]);
        for(int i=0; i<comm.length; i++){
        log.info(" "+comm[i]);
        }
        log.info("Command:");
        return exec(comm);
    }

    private static boolean exec(String [] command){
        Process proc = null;
        try{
         proc = Runtime.getRuntime().exec(command);

        }catch(IOException e){
          log.error("Something bad happened while trying to execute process!");
        }
        int exitStatus;
        while(true){
            try{
                exitStatus = proc.waitFor();
                break;
            }
            catch(InterruptedException e){
                log.error("Process was interrupted!");
                exitStatus = 1;
                break;
            }
            catch(NullPointerException e){
                log.error("Process object is null!");
                exitStatus = 1;
                break;
            }
        }

        return (exitStatus==0);
    }


}
